package manager;

import beans.ManagerPageBean;
import dtos.*;
import utils.*;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@ManagedBean(name = "tb")
@SessionScoped
public class TicketBean implements Serializable {
    private static final long serialVersionUID = -6625353284053378621L;

    @EJB
    private ResticketDAORemote rtr;

    @EJB
    private TechticketDAORemote ttr;

    @EJB
    private ResponseDAORemote rr;

    @EJB
    private ResourceDAORemote rdr;

    @EJB
    private UserDAORemote ur;

    @EJB
    private TechresourceDAORemote trr;

    @EJB
    private DomainDAORemote dr;

    @EJB
    private MeasureunitDAORemote mudr;

    @ManagedProperty(value = "#{managerPageBean}")
    private ManagerPageBean mpb;

    private String[] ticketFrom = {"-none-", "Basic user", "Technical Support", "Leave of absence"};
    private String ticketType = "-none-";
    private List<ResticketDTO> resTickets;
    private List<TechticketDTO> techTickets;
    private boolean fromUser;
    private boolean fromTech;
    private List<UserDTO> users;
    private ResticketDTO resT;
    private TechticketDTO techT;
    private boolean var1;
    private boolean var2;
    private String response;
    private UserDTO manager;
    private UserDTO leaveUser;

    @PostConstruct
    public void init() {
        manager = (UserDTO) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("loggedUser");
        if (manager.getIdDomain() == 1) {
            techTickets = ttr.getAllTechtickets();
            resTickets = rtr.getAllRestickets();
        } else {
            techTickets = ttr.getTechticketsByUserDomain(manager);
            resTickets = rtr.getResticketsByUserDomain(manager);
        }
        var1 = false;
        var2 = false;
        users = ur.getAllUsersDTO();
    }

    public UserDTO getManager() {
        manager = (UserDTO) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("loggedUser");
        return manager;
    }

    public void setManager(UserDTO manager) {
        this.manager = manager;
    }

    public ManagerPageBean getMpb() {
        return mpb;
    }

    public void setMpb(ManagerPageBean mpb) {
        this.mpb = mpb;
    }

    public List<ResticketDTO> getResTickets() {
        return resTickets;
    }

    public void setResTickets(List<ResticketDTO> resTickets) {
        this.resTickets = resTickets;
    }

    public List<TechticketDTO> getTechTickets() {
        return techTickets;
    }

    public void setTechTickets(List<TechticketDTO> techTickets) {
        this.techTickets = techTickets;
    }

    public boolean isFromUser() {
        return fromUser;
    }

    public void setFromUser(boolean fromUser) {
        this.fromUser = fromUser;
    }

    public String[] getTicketFrom() {
        return ticketFrom;
    }

    public void setTicketFrom(String[] ticketFrom) {
        this.ticketFrom = ticketFrom;
    }

    public boolean isFromTech() {
        return fromTech;
    }

    public void setFromTech(boolean fromTech) {
        this.fromTech = fromTech;
    }

    public boolean isVar1() {
        return var1;
    }

    public void setVar1(boolean var1) {
        this.var1 = var1;
    }

    public boolean isVar2() {
        return var2;
    }

    public void setVar2(boolean var2) {
        this.var2 = var2;
    }

    public TechticketDTO getTechT() {
        return techT;
    }

    public void setTechT(TechticketDTO techT) {
        this.techT = techT;
    }

    public ResticketDTO getResT() {
        return resT;
    }

    public void setResT(ResticketDTO resT) {
        this.resT = resT;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public void goBack() {
        if (manager.getIdDomain() == 1) {
            techTickets = ttr.getAllTechtickets();
            resTickets = rtr.getAllRestickets();
        } else {
            techTickets = ttr.getTechticketsByUserDomain(manager);
            resTickets = rtr.getResticketsByUserDomain(manager);
        }
    }

    public void setTicketId(ActionEvent event) {
        response = "";
        int a = Integer.parseInt(event.getComponent().getAttributes()
                .get("ticketid").toString());
        if (event.getComponent().getAttributes().get("tickettype").toString()
                .equals("request")) {
            resT = rtr.getResticket(a);
            var1 = true;
            var2 = false;
        } else {
            techT = ttr.getTechticket(a);
            System.out.println(techT.getIdTechresource());
            var1 = false;
            var2 = true;
        }
        mpb.setPage("createResponse");
    }

    public void doResponse(ActionEvent event) {
        if (event.getComponent().getAttributes().get("tickettype").toString()
                .equals("request")) {
            ResponseDTO resp = new ResponseDTO();
            resp.setDescription(response);
            resp.setDate(new Date());
            resp.setIdUser(manager.getId());
            rr.insertResponse(resp);
            List<ResponseDTO> answers = rr.getAllResponsesByUserId(manager
                    .getId());
            for (ResponseDTO r : answers) {
                if (r.getDescription().equals(resp.getDescription())) {
                    resT.setIdResponse(r.getId());
                    resT.setStatus("Processed");
                    rtr.updateResticket(resT);
                }
            }
        } else {
            ResponseDTO resp = new ResponseDTO();
            resp.setDescription(response);
            resp.setDate(new Date());
            resp.setIdUser(manager.getId());
            rr.insertResponse(resp);
            List<ResponseDTO> answers = rr.getAllResponsesByUserId(manager
                    .getId());
            for (ResponseDTO r : answers) {
                if (r.getDescription().equals(resp.getDescription())) {
                    techT.setIdResponse(r.getId());
                    techT.setStatus("Finalized");
                    ttr.updateTechticket(techT);
                }
            }
        }
        mpb.setPage("manageRequest");
        goBack();
    }

    public void renderTables(ValueChangeEvent event) {
        if (manager.getIdDomain() == 1) {
            techTickets = ttr.getAllTechtickets();
            resTickets = rtr.getAllRestickets();
        } else {
            techTickets = ttr.getTechticketsByUserDomain(manager);
            resTickets = rtr.getResticketsByUserDomain(manager);
        }
        if (event.getNewValue().toString().equals("Basic user")) {
            fromUser = true;
            fromTech = false;
        } else if (event.getNewValue().toString().equals("Technical Support")) {
            fromUser = false;
            fromTech = true;
        } else {
            fromUser = false;
            fromTech = false;
        }
    }

    public UserDTO getUser(int id) {
        return ur.getUser(id);
    }

    public ResourceDTO getResource(int id) {
        return rdr.getResource(id);
    }

    public TechresourceDTO getTechresource(int id) {
        return trr.getTechresourcesById(id);
    }

    public DomainDTO getDomain(int id) {
        return dr.getDomain(id);
    }

    public MeasureunitDTO getMeasureunit(int id) {
        return mudr.getMeasureunit(id);
    }

    public String getTicketType() {
        return ticketType;
    }

    public void setTicketType(String ticketType) {
        this.ticketType = ticketType;
    }

    public UserDTO getLeaveUser() {
        return leaveUser;
    }

    public void setLeaveUser(UserDTO leaveUser) {
        this.leaveUser = leaveUser;
    }

    public List<UserDTO> getUsers() {
        return users;
    }

    public void setUsers(List<UserDTO> users) {
        this.users = users;
    }
}

